package com.library.workbench.web.controller.admin;

import com.library.commons.constant.Constant;
import com.library.commons.domain.ReturnObject;
import com.library.commons.utils.DateTimeUtils;
import com.library.commons.utils.UUIDUtils;
import com.library.settings.pojo.User;
import com.library.settings.service.UserService;
import com.library.workbench.pojo.Book;
import com.library.workbench.pojo.Borrow;
import com.library.workbench.service.BookService;
import com.library.workbench.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class BorrowAndReturnController {
    @Autowired
    private BorrowService borrowService;

    @Autowired
    private UserService userService;

    @Autowired
    private BookService bookService;

    @RequestMapping("/workbench/admin/borrowAndReturn/toBorrow.do")
    public String toBorrow(){
        return "/workbench/admin/borrowAndReturn/borrow";
    }

    @RequestMapping("/workbench/admin/borrowAndReturn/queryExistedBookByActNameAuthor.do")
    @ResponseBody
    public Object queryExistedBookByActNameAuthor(String bookAccount,String bookName,String author,Integer pageNo,Integer pageSize){
        // 封装对象
        Map<String,Object> map=new HashMap<>();
        map.put("bookName",bookName);
        map.put("author",author);
        map.put("bookAccount",bookAccount);
        map.put("beginNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        // 调用service层方法
        List<Book> books = borrowService.queryExistBookForConditionForPage(map);
        int totalRows = borrowService.queryExistBookByConditionForRows(map);
        // 封装参数
        map.put("books",books);
        map.put("totalRows",totalRows);
        return map;
    }

    @RequestMapping("/workbench/admin/borrowAndReturn/getUserName.do")
    @ResponseBody
    public Object getUserName(String userAccount){
        // 调用service层方法
        User user = userService.queryUserByAccount(userAccount);
        ReturnObject returnObject=new ReturnObject();
        if (user==null){
            returnObject.setCode(Constant.RETURN_FAIL);
        }else {
            returnObject.setCode(Constant.RETURN_SUCCESS);
            returnObject.setOtherObject(user);
        }
        // 生成响应信息
        return returnObject;
    }

    @RequestMapping("/workbench/admin/borrowAndReturn/insertBorrow.do")
    @ResponseBody
    public Object insertBorrow(String bookId,String userAccount,String endDate){
        // 封装参数
        Borrow borrow=new Borrow();
        borrow.setBorrowId(UUIDUtils.getUUID());
        borrow.setBookId(bookId);
        borrow.setBorrowDate(DateTimeUtils.formatDate(new Date()));
        borrow.setEndTime(endDate);
        borrow.setUserId(userService.queryUserByAccount(userAccount).getUserId());
        ReturnObject returnObject=new ReturnObject();
        try {
            // 调用service层方法
            int res = borrowService.saveBorrow(borrow);
            Book book = bookService.queryBookByBookId(bookId);
            book.setExist("0");
            res+=bookService.updateBookByBookId(book);
            if (res>0){
                returnObject.setCode(Constant.RETURN_SUCCESS);
            }else{
                returnObject.setCode(Constant.RETURN_FAIL);
                returnObject.setMessage("系统繁忙，请稍候再试....");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constant.RETURN_FAIL);
            returnObject.setMessage("系统繁忙，请稍候再试....");
        }
        return returnObject;
    }

    @RequestMapping("/workbench/admin/borrowAndReturn/queryNotReturnBookInfo.do")
    @ResponseBody
    public Object queryNotReturnBookInfo(String userId){
        System.out.println(userId);
        List<Book> books=new ArrayList<>();
        ReturnObject returnObject=new ReturnObject();
        // 调用service层方法
        List<Borrow> borrows = borrowService.queryBorrowInfoByUserIdAndNotReturn(userId);
        Book book=null;
        if (borrows==null||borrows.size()<1){
            returnObject.setCode(Constant.RETURN_FAIL);
            returnObject.setMessage("该用户暂无需要归还的书籍！");
        }else{
            returnObject.setCode(Constant.RETURN_SUCCESS);
            // 遍历借阅记录
            for (Borrow borrow:borrows){
                book=bookService.queryBookByBookId(borrow.getBookId());
                book.setBorrowDate(borrow.getBorrowDate());
                book.setBorrowId(borrow.getBorrowId());
                books.add(book);
            }
            returnObject.setOtherObject(books);
        }
        // 返回响应信息
        return returnObject;
    }

    @RequestMapping("/workbench/admin/borrowAndReturn/returnBook.do")
    @ResponseBody
    public Object returnBook(String bookId,String borrowId){
        ReturnObject returnObject=new ReturnObject();
        // 调用service层方法
        try {
            // 更新书籍信息
            Book book = bookService.queryBookByBookId(bookId);
            book.setExist("1");
            int res=bookService.updateBookByBookId(book);

            // 更新归还信息
            Borrow borrow = borrowService.queryBorrowById(borrowId);
            borrow.setReturnTime(DateTimeUtils.formatDate(new Date()));
            if (borrow.getEndTime().compareTo(borrow.getReturnTime())>0){
                borrow.setIslater("0");
            }else{
                borrow.setIslater("1");
            }
            res+=borrowService.saveReturnBorrowInfo(borrow);
            if (res==2){
                returnObject.setCode(Constant.RETURN_SUCCESS);
            }else{
                returnObject.setCode(Constant.RETURN_FAIL);
                returnObject.setMessage("系统繁忙，请稍候再试....");
            }

        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constant.RETURN_FAIL);
            returnObject.setMessage("系统繁忙，请稍候再试....");
        }

        return returnObject;
    }
}
